package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.linkage.educloud.ah.MyApplication;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.AccountData;

import java.util.ArrayList;
import java.util.List;

/**
 * <desc>
 * Created by The Moss on 2018/8/27.
 */

public class MyAccountPopWindowAdapter extends BaseAdapter {
    Context mContext;
    List<AccountData> datas;
    PopWindonItemListener mListener;
    public interface PopWindonItemListener{
        void onClickItem(int position);
        void onClickDelete(int position);
    }
    public MyAccountPopWindowAdapter(Context mContext) {
        this.mContext = mContext;
        datas = new ArrayList<>();
    }
    public void setDatas(List<AccountData> data){
        this.datas.addAll(data);
        notifyDataSetChanged();
    }
    public void setOnclickListener(PopWindonItemListener listener){
        if (listener != null) {
            mListener = listener;
        }
    }
    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public String getItem(int position) {
        return datas.get(position).getLoginAccount();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_popup_account, null);
            holder = new ViewHolder();
            holder.nameText = convertView.findViewById(R.id.item_pop_name);
            holder.lineView = convertView.findViewById(R.id.item_pop_line);
            holder.deleteImage = convertView.findViewById(R.id.item_pop_delete);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (position == getCount() - 1) {
            holder.lineView.setVisibility(View.GONE);
        } else {
            holder.lineView.setVisibility(View.VISIBLE);
        }
        holder.nameText.setText(getItem(position));
        holder.deleteImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    int delete = MyApplication.getInstance().getResovler().deleteAccount(getItem(position));
                    if (delete > 0) {
                        datas.remove(position);
                        notifyDataSetChanged();
                        mListener.onClickDelete(position);
                    }
                }
            }
        });
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onClickItem(position);
                }
            }
        });
        return convertView;
    }

    private class ViewHolder {
        TextView nameText;
        View lineView;
        ImageView deleteImage;
    }
}
